#include "weather_label.h"
#include <QMouseEvent>
#include <algorithm>

weather_label::weather_label(QWidget *parent) : QLabel(parent)
{
    setMinimumSize(400, 200);
}

void weather_label::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
        
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);
        
        // 绘制背景
        painter.fillRect(rect(), QColor(212, 241, 249));
        
        // 计算坐标点
        calculatePoints();
        
        // 先绘制低温曲线(蓝色)
        QPen pen(m_lowColor, m_curveWidth);
        painter.setPen(pen);
        
        QPainterPath lowPath;
        lowPath.moveTo(m_lowPoints[0]);
        for (int i = 1; i < 7; ++i) {
            lowPath.lineTo(m_lowPoints[i]);
        }
        painter.drawPath(lowPath);
        
        // 再绘制高温曲线(橙色)
        pen.setColor(m_highColor);
        painter.setPen(pen);
        
        QPainterPath highPath;
        highPath.moveTo(m_highPoints[0]);
        for (int i = 1; i < 7; ++i) {
            highPath.lineTo(m_highPoints[i]);
        }
        painter.drawPath(highPath);
        
        // 绘制温度点 - 先低温后高温
        QBrush brush;
        brush.setStyle(Qt::SolidPattern);
        
        // 绘制低温点(蓝色)
        brush.setColor(m_lowColor);
        painter.setBrush(brush);
        pen.setColor(m_lowColor);
        painter.setPen(pen);
        
        for (int i = 0; i < 7; ++i) {
            painter.drawEllipse(m_lowPoints[i], m_pointSize/2, m_pointSize/2);
        }
        
        // 绘制高温点(橙色)
        brush.setColor(m_highColor);
        painter.setBrush(brush);
        pen.setColor(m_highColor);
        painter.setPen(pen);
        
        for (int i = 0; i < 7; ++i) {
            painter.drawEllipse(m_highPoints[i], m_pointSize/2, m_pointSize/2);
        }
        
        // 绘制温度文本
        QFont font = painter.font();
        font.setPointSize(9);
        painter.setFont(font);
        
        // 高温文本(橙色)
        painter.setPen(m_highColor);
        for (int i = 0; i < 7; ++i) {
            QString text = QString("%1°").arg(m_highTemps[i]);
            QRect textRect(m_highPoints[i].x() - 20, m_highPoints[i].y() - 25, 40, 20);
            painter.drawText(textRect, Qt::AlignCenter, text);
        }
        
        // 低温文本(蓝色)
        painter.setPen(m_lowColor);
        for (int i = 0; i < 7; ++i) {
            QString text = QString("%1°").arg(m_lowTemps[i]);
            QRect textRect(m_lowPoints[i].x() - 20, m_lowPoints[i].y() + 5, 40, 20);
            painter.drawText(textRect, Qt::AlignCenter, text);
        }
}

void weather_label::setWeatherData(const QList<Forecast>& forecasts)
{
    if (forecasts.size() < 7) return;
        
        for (int i = 0; i < 7; ++i) {
            // 提取高温数字部分
            QString highStr = forecasts[i].high;
            QRegExp highRegExp("(\\d+)");
            if (highRegExp.indexIn(highStr) != -1) {
                m_highTemps[i] = highRegExp.cap(1).toInt();
            } else {
                m_highTemps[i] = 0; // 默认值
            }
            
            // 提取低温数字部分
            QString lowStr = forecasts[i].low;
            QRegExp lowRegExp("(\\d+)");
            if (lowRegExp.indexIn(lowStr) != -1) {
                m_lowTemps[i] = lowRegExp.cap(1).toInt();
            } else {
                m_lowTemps[i] = 0; // 默认值
            }
        }
        
        update();
}

void weather_label::calculatePoints()
{
    // 计算X坐标
    int widthStep = width() / 8;
    for (int i = 0; i < 7; ++i) {
        m_highPoints[i].setX(widthStep * (i + 1));
        m_lowPoints[i].setX(widthStep * (i + 1));
    }
    
    // 计算Y坐标
    int maxTemp = *std::max_element(m_highTemps, m_highTemps + 7);
    int minTemp = *std::min_element(m_lowTemps, m_lowTemps + 7);
    int tempRange = maxTemp - minTemp;
    
    if (tempRange == 0) tempRange = 1; // 避免除以0
    
    int padding = 50;
    int availableHeight = height() - 2 * padding;
    
    for (int i = 0; i < 7; ++i) {
        m_highPoints[i].setY(padding + availableHeight - 
                           (m_highTemps[i] - minTemp) * availableHeight / tempRange);
        m_lowPoints[i].setY(padding + availableHeight - 
                          (m_lowTemps[i] - minTemp) * availableHeight / tempRange);
    }
}

void weather_label::setHighCurveColor(const QColor &color)
{
    m_highColor = color;
    update();
}

void weather_label::setLowCurveColor(const QColor &color)
{
    m_lowColor = color;
    update();
}

void weather_label::setCurveWidth(int width)
{
    m_curveWidth = width;
    update();
}

void weather_label::setPointSize(int size)
{
    m_pointSize = size;
    update();
}

void weather_label::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    update();
}
